<?php


namespace App\Services;


use App\Models\Member;
use Hedeqiang\UMeng\Android;
use Hedeqiang\UMeng\IOS;

/**
 * Class UmengService
 * @package App\Services
 * @see https://github.com/hedeqiang/UMeng-Push
 */
class UmengService extends BaseService
{
    private $oAndroid;
    private $oIos;

    private $type;
    private $androidConf;
    private $iosConf;

    private $paramsBuilder;

    /**
     * UmengService constructor.
     * @param string $type 推送类型 owner|driver
     * @throws \Exception
     */
    public function __construct($type)
    {
        parent::__construct();

        if (empty($type)) {
            throw new \Exception('type must be driver or owner');
        }
        $this->type = $type;

        $this->androidConf = config("umeng.{$this->type}.android");
        $this->iosConf     = config("umeng.{$this->type}.ios");

        $this->oAndroid = new Android($this->androidConf);
        $this->oIos     = new IOS($this->iosConf);
    }

    /**
     * 参数构建
     *
     * @param array $params
     * @return $this
     * @throws \Exception
     */
    public function paramsBuilder(array $params)
    {
        if (!sizeof($params)) {
            throw new \Exception('empty parameters');
        }

        $this->paramsBuilder = $params;

        return $this;
    }

    /**
     * 发送调用
     *
     * @param $oMember
     * @throws \Exception
     */
    public function easyCustomizedCast($oMember)
    {
        if (is_null($oMember)) {
            throw new \Exception('illegal param');
        }

        switch ($oMember->user_platform) {
            case Member::USER_PLATFORM_ANDROID:
                $this->androidCustomizedCast();
                break;
            case Member::USER_PLATFORM_IOS:
                $this->iosCustomizedCast();
                break;
        }
    }

    /**
     * 安卓customized cast
     *
     * @return mixed
     * @throws \Exception
     */
    public function androidCustomizedCast()
    {
        $params = [
            'alias_type'  => 'user_alias',
            'alias'       => 'bfb6bc3b-c011-3987-ac50-1a69748e2b09',
            "ticker"      => "测试提示文字",
            "title"       => "测试标题",
            "text"        => "测试文字描述",
            "after_open"  => "go_app",
            "description" => "测试广播通知-Android",
        ];

        return $this->oAndroid->sendAndroidCustomizedcast($this->paramsBuilder);
    }

    /**
     * ios customized cast
     *
     * @return mixed
     * @throws \Exception
     */
    public function iosCustomizedCast()
    {
        $params = [
            'alert'       => [
                'title' => 'title',
                'body'  => 'body',
            ],  //字符串或者JSON
            'alias_type'  => 'user_alias',
            'alias'       => 'bfb6bc3b-c011-3987-ac50-1a69748e2b09',
            "description" => "测试alias通知-iOS"
        ];

        return $this->oIos->sendIOSCustomizedcast($this->paramsBuilder);
    }

    /**
     * @return mixed
     * @throws \Exception
     */
    public function iosBroadcast()
    {
        return $this->oIos->sendIOSBroadcast($this->paramsBuilder);
    }


}